package com.link.base.base.errorcode.controller;

import com.link.base.base.errorcode.model.ErrorCodeMessage;
import com.link.base.base.errorcode.service.ErrorCodeMessageService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 错误编码信息维护
 *
 * @author cuizhiyong
 * @date 2018/11/12 9:46
 */

@RestController
@RequestMapping("/link/errorCodeMessage")
public class ErrorCodeMessageController extends BasicController<ErrorCodeMessage> {

    @Resource
    private ErrorCodeMessageService errorCodeMessageService;

    @Override
    public BasicService<ErrorCodeMessage> getBasicService() throws Exception {
        return errorCodeMessageService;
    }

    /**
     * 更新错误编码数据的Redis缓存
     *
     * @author cuizhiyong
     * @date 2019/4/26 11:09
     */
    @GetMapping("/refreshCache")
    public Object refreshRedisCache() {
        Map<String, Object> result = new HashMap<>();
        errorCodeMessageService.refreshRedisCache();
        result.put("success", true);
        return result;
    }

    @Override
    public void beforUpsert(ErrorCodeMessage entity, HttpServletRequest request) throws Exception {
        if (StringUtils.isBlank(entity.getErrorLang())) {
            throw new ServiceException("ERROR-004");
        }
        if (StringUtils.isBlank(entity.getErrorMessage())) {
            throw new ServiceException("ERROR-005");
        }
        if (errorCodeMessageService.duplicateValidate(entity)) {
            throw new ServiceException("ERROR-006");
        }
    }
}
